package com.example.userservice.permission.checker;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.userservice.entity.User;
import com.example.userservice.entity.vo.UserVo;
import com.example.userservice.feign.PermissionFeign;
import com.example.userservice.mapper.UserMapper;
import com.example.userservice.permission.PermissionChecker;
import com.example.userservice.util.UserConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component("super_admin")
public class SuperAdminPermission implements PermissionChecker {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<UserVo> getUsers(Long currentUserId) {
        List<User> users = userMapper.selectAll();
        List<UserVo> list = UserConverter.getUserVoListFromUserList(users);
        return list;
    }

    @Override
    public UserVo getUser(Long currentUserId, Long targetUserId) {
        User user = userMapper.selectByUserId(targetUserId);
        UserVo userVo = UserConverter.getUserVoFromUser(user);
        return userVo;
    }

    @Override
    public Boolean updateUser(Long currentUserId, Long targetUserId, User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", targetUserId)
                .set("phone",user.getPhone())
                .set("email",user.getEmail());
        return userMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public Boolean resetPassword(Long currentUserId, User user) {
        Long targetUserId = userMapper.selectUserIdByUserName(user.getUserName());
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", targetUserId).set("password", user.getPassword());
        return userMapper.update(null, updateWrapper) > 0;
    }
}
